﻿using System;
using System.IO;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.ProcessInterfaces;

namespace XWG.JWBST.OnelineAlarmFileProcess
{
    /// <summary>
    /// 在线报警附件上传
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 在线报警附件上传
        /// </summary>
        /// <param name="reader">接受数据</param>
        /// <param name="context">请求信息</param>
        /// <returns>操作结果</returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string receivemes = reader.ReadToEnd();

            // UrlDecode解码
            receivemes = HttpUtility.UrlDecode(receivemes);
            JObject oka = (JObject)JsonConvert.DeserializeObject(receivemes);

            TbBstOnlineAlarmFile onLineFlie = new TbBstOnlineAlarmFile();
            onLineFlie.AlarmFilePkid = Guid.NewGuid().ToString();
            onLineFlie.OnlineAlarmPkid = oka["onlineID"].ToString().Replace("\"", string.Empty);
            onLineFlie.Filetype = oka["filetype"].ToString().Replace("\"", string.Empty);
            onLineFlie.Upfile = (byte[])oka["file"];
            //onLineFlie.Upfile = Convert.FromBase64String(oka["file"].ToString().Replace("\"", string.Empty));
            onLineFlie.UploadDate = DateTime.Now;

            //检查上传在线报警或线索举报ID是否存在
            if (XWG.JWBST.JWBSTBLL.TbBstOnlineAlarmFileBLL.IsExist(onLineFlie.OnlineAlarmPkid))
            {
                //返回操作结果
                ResultModel re = new ResultModel();
                re = XWG.JWBST.JWBSTBLL.TbBstOnlineAlarmFileBLL.Insert(onLineFlie, string.Empty, false);
                return re.Message;
            }
            else
            {
                return "该附件未找到对应信息";
            }
        }
    }
}
